--
-- Author: shunguo.chen
-- Date: 2018-06-27 20:10:59
--
-- BlessRender
--

local ClassRef = app.mvc.createRenderClass(...)

local BLESS_EXP_STR = L("lua_code_text_229")
local FAME_LV_STR = L("lua_code_text_333")

-- 不同占领者的名字颜色
local MY_TOWN_COLOR = cc.c3b(106, 180, 255)
local OTHER_TOWN_COLOR = cc.c3b(255, 100, 100)
local FREE_TOWN_COLOR = cc.c3b(255, 210, 0)

local string_format = string.format

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = ZoneInfo
function ClassRef:init(data)
    self.super.init(self, data)

    display.uiAddClick(self, function()
        if self._data then
            executeFunc(self._onClick, self)
        end
    end, true, 0.01)

    self._ui.map:setVisible(true)

    -- TODO csg  guild icon
    self._ui.guild_icon:setVisible(false)
    self._guideIcon1 = self._ui.guild_icon:getChildByName("icon_u")
    self._guideIcon2 = self._ui.guild_icon:getChildByName("icon_b")

    local bar = self._ui.bar
    self._fameNameText = bar:getChildByName("renown")
    self._fameNumText = bar:getChildByName("num")
    self._fameBar = bar:getChildByName("LoadingBar")

    self:refresh()

    self.preBlessExp = 0
    self.blessMaxExp = 0

    self.isFameMaxLv = false
    self.preFameLv = 0
    self.preFameExp = 0
    self.preFameMax = 0
end

function ClassRef:refresh()
    if self._data then
        -- self._ui.bg:loadTexture()

        local zoneInfo = self._data
        self._ui.map:loadTexture(zoneInfo:getIcon())
        self._ui.name:setString(zoneInfo:getName())
        self._ui.tips:setString(zoneInfo:getOwnerStr())
        local bc = app:getInst("BiddingController")
        local gname = bc:getTownOwnerName(zoneInfo.id)
        if gname then
            self._ui.tips:setString(string.format(L("lua_code_text_469"), gname))
        end

        local curExp, maxExp = zoneInfo:getBlessExp()
        self._ui.num:setString(string_format(BLESS_EXP_STR, curExp, maxExp))
        self.preBlessExp = curExp
        self.blessMaxExp = maxExp

        local fame = zoneInfo:getUDFame()
        local isMaxLv = fame:isMaxLv()
        self._fameNameText:setString(string_format(FAME_LV_STR, fame:getLvName()))
        if isMaxLv then
            self._fameNumText:setString("MAX")
            self._fameBar:setPercent(100)
        else
            curExp, maxExp = fame:getLvExp()
            self._fameNumText:setString(string_format("%s/%s", curExp, maxExp))
            self._fameBar:setPercent(curExp/maxExp * 100)

            self.preFameExp = curExp
            self.preFameLv = fame.level
            self.preFameMax = maxExp
        end

        self.isFameMaxLv = isMaxLv
    end
end

function ClassRef:playAction()
    local maxTimes = 30
    local curTimes = 0

    -- bless action params
    local curBlessExp = self._data:getBlessExp()
    local preBlessExp = self.preBlessExp
    local blessMaxExp = self.blessMaxExp
    local blessStep = (curBlessExp - preBlessExp) / maxTimes

    -- fame action stepDatas
    local stepDatas, step
    local fame
    local preFameExp, preFamePer, curFameExp, curFameMax
    local fameStep, famePerStep
    if not self.isFameMaxLv then
        stepDatas = {}
        step = 1
        fame = self._data:getUDFame()
        curFameExp, curFameMax = fame:getLvExp()
        local curFameLv = fame.level

        local preFameMax = self.preFameMax
        local preFameLv = self.preFameLv
        local fameChange, famePerChange
        preFameExp = self.preFameExp
        preFamePer = preFameExp / preFameMax * 100
        if preFameLv == curFameLv then
            -- 没有跨级
            fameChange = curFameExp - preFameExp
            famePerChange = curFameExp / curFameMax * 100 - preFamePer
            stepDatas[#stepDatas + 1] = {exp=curFameExp, expMax=preFameMax, level=preFameLv}
        else
            -- 最多只能夸一级
            fameChange = preFameMax - preFameExp + curFameExp
            famePerChange = 100 - preFamePer + curFameExp / curFameMax * 100
            stepDatas[#stepDatas + 1] = {exp=preFameExp, expMax=preFameMax, level=preFameLv}
            stepDatas[#stepDatas + 1] = {exp=curFameExp, expMax=curFameMax, level=curFameLv}
        end

        fameStep = fameChange / maxTimes
        famePerStep = famePerChange / maxTimes
    end

    -- local node
    local numText = self._ui.num
    local fameNumText = self._fameNumText
    local fameBar = self._fameBar

    self:stopAllActions()
    self:schedule(function()
        curTimes = curTimes + 1

        if curTimes < maxTimes then
            preBlessExp = preBlessExp + blessStep
            numText:setString(string_format(BLESS_EXP_STR, math.ceil(preBlessExp), blessMaxExp))

            if not self.isFameMaxLv then
                preFameExp = preFameExp + fameStep
                preFamePer = preFamePer + famePerStep

                local expMax = stepDatas[step].expMax
                if preFameExp < expMax then
                    fameNumText:setString(string_format("%s/%s", math.ceil(preFameExp), expMax))
                    fameBar:setPercent(preFamePer)
                elseif step < #stepDatas then
                    preFameExp = preFameExp - expMax
                    preFamePer = preFamePer - 100
                    step = step + 1
                    expMax = stepDatas[step].expMax
                    fameNumText:setString(string_format("%s/%s", math.ceil(preFameExp), expMax))
                    fameBar:setPercent(preFamePer)
                    self._fameNameText:setString(string_format(FAME_LV_STR, fame:getLvName()))
                end
            end
        else
            self:stopAllActions()
            numText:setString(string_format(BLESS_EXP_STR, curBlessExp, blessMaxExp))
            if not self.isFameMaxLv then
                fameNumText:setString(string_format("%s/%s", curFameExp, curFameMax))
                fameBar:setPercent(curFameExp/curFameMax * 100)
                self._fameNameText:setString(string_format(FAME_LV_STR, fame:getLvName()))
            end
        end
    end, 0.05)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
